<?php
session_start();
require_once('Connections/paypark.php');
mysql_select_db($database_grues, $grues); 
if(isset($_POST["camptempsafegit"])){
	
	$query2 = "SELECT * FROM vehicles LEFT JOIN estacionaments ON vehicles.codivehicle=estacionaments.vehicle where empresa='".$_SESSION["usuari"]."' order by estacionaments.codi desc LIMIT 1";
$detall2=mysql_query($query2, $grues) or die(mysql_error());
$dades2 = mysql_fetch_assoc($detall2);
$total2=mysql_num_rows($detall2);
	$temps=substr($_POST["camptempsafegit"],0,2)*60+substr($_POST["camptempsafegit"],3,2);
if($_POST["accio"]=="nouestacionament"){
		$resultat=mysql_query('insert into estacionaments(vehicle, durada,coordenades) values ("'.$dades2["codivehicle"].'","'.$temps.'","'.$_POST["pos"].'")' , $grues) or die(mysql_error());
}
else{
	
	$resultat=mysql_query('update estacionaments set durada=durada+"'.$temps.'" where codi="'.$dades2["codi"].'"' , $grues) or die(mysql_error());
}
$resultat=mysql_query('update empreses set saldo=saldo-"'.str_replace(",",".",$_POST["campdespesa"]).'" where codiempresa="'.$_SESSION["usuari"].'"' , $grues) or die(mysql_error());
$query2 = "SELECT * FROM vehicles LEFT JOIN estacionaments ON vehicles.codivehicle=estacionaments.vehicle where empresa='".$_SESSION["usuari"]."' order by estacionaments.codi desc LIMIT 1";
$detall2=mysql_query($query2, $grues) or die(mysql_error());
$dades2 = mysql_fetch_assoc($detall2);
$total2=mysql_num_rows($detall2);
}
else{
	if(isset($_POST["usuari"])){
		$query = "SELECT * FROM usuaris where login='".$_POST["usuari"]."' and password='".$_POST["password"]."' LIMIT 1";
		$detall=mysql_query($query, $grues) or die(mysql_error());
		$dades = mysql_fetch_assoc($detall);
		$total=mysql_num_rows($detall);
		if($total==0){
			header("Location:index.php");
		}
	}else{
		header("Location:index.php");
		
	}
	$_SESSION["usuari"]=$dades["empresa"];
	$query2 = "SELECT * FROM vehicles LEFT JOIN estacionaments ON vehicles.codivehicle=estacionaments.vehicle where empresa='".$dades["empresa"]."' order by estacionaments.codi desc LIMIT 1";
$detall2=mysql_query($query2, $grues) or die(mysql_error());
$dades2 = mysql_fetch_assoc($detall2);
$total2=mysql_num_rows($detall2);
}


$to_time=strtotime("now");
$from_time=strtotime($dades2["entrada"]);
$dif=round(($from_time - $to_time) / 60,2)+$dades2["durada"];
$hora=str_pad(floor(abs($dif/60)), 2, "0", STR_PAD_LEFT).':'.str_pad(round(abs($dif)-(floor(abs($dif/60))*60)), 2, "0", STR_PAD_LEFT);
if($dif>15){
$resultat="ok";	
$icona="ok";

$temps='<p id="temps" style="color:#C5D8E1">'.$hora.'</p>';
$plana="afegirtemps";
}
else if($dif>0){
	$resultat="alerta";	
	$icona="alerta";
$temps='<p id="temps" style="color:#EDC97A">'.$hora.'</p>';
$plana="afegirtemps";
}
else if($dif<-200){
	$resultat="no";	
	$icona="ok";
$temps='<p id="temps" style="color:#C5D8E1">--:--</p>';
$plana="nouestacionament";
}
else{
	$resultat="pasat";	
	$icona="pasat";	
$temps='<p id="temps" style="color:#FF0000">'.$hora.'</p>';
$plana="afegirtemps";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
    <meta name="apple-mobile-web-app-capable" content="yes" />  
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />  
        <link rel="apple-touch-icon" href="img/icon.png"/> 
<title>PayPark</title>
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<link href="css/estils.css" rel="stylesheet" type="text/css" />

<script language="javascript" src="js/jquery-1.5.1.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script language="javascript" src="js/infobox.js"></script>
</head>

<body>
<div id="contingut">
<div id="mapa">

</div>
<div id="dadesestacionament">
<form name="formafegirtemps" method="post" action="afegirtemps.php">
</form>
<form name="formnouestacionament" method="post" action="nouestacionament.php">
<input type="hidden" id="camppos" name="pos" value="" />
</form>
<img src="img/icon_<?php echo $icona;?>.png" /><img src="img/icona_vehicle.png" /><p id="matricula"><?php echo $dades2["matricula"];?></p><a href="#" onclick="document.forms['form<?php echo $plana;?>'].submit();return false;"><img src="img/icona_rellotge.png" /></a><?php echo $temps;?>
</div>
</div>
<script type="text/javascript">
function onSuccessGeolocating(position)
		{
			var latlng = new google.maps.LatLng(position.coords.latitude, 
 			                                          position.coords.longitude);

			

			var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapa"), myOptions);
	var txt=new String(latlng);
	document.forms["formnouestacionament"].camppos.value=txt.substr(1,txt.length-2);
	var marker = new google.maps.Marker({
		  position: latlng,
		  map: map,
		  icon: "img/marcador.png"
	  });
	  if (geocoder) {
		  
    

      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            infowindow.setContent("<p style='color:#000;font-size:12px;'>"+results[0].address_components[1].long_name+" "+results[0].address_components[0].long_name+"<br />"+results[0].address_components[6].long_name+" "+results[0].address_components[2].long_name+"</p>");
            infowindow.open(map, marker);
          }
        } else {
          alert("Geocoder failed due to: " + status);
        }
      });
    }
		}
	
function onErrorGeolocating(position){
}
  var geocoder = new google.maps.Geocoder();
   var infowindow = new google.maps.InfoWindow();
	<?php if($resultat=="no"){?>
	if(navigator.geolocation)  {
		navigator.geolocation.getCurrentPosition(onSuccessGeolocating,  
                                         onErrorGeolocating ); 
	}
	else{
    var latlng = new google.maps.LatLng(32,20);
	var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapa"), myOptions);
	var marker = new google.maps.Marker({
		  position: latlng,
		  map: map,
		  icon: "img/marcador.png"
	  });
	  if (geocoder) {
		  
    

      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            infoBox = new InfoBox({latlng: marker.getPosition(), map: map,inner:"<p style='color:#FFF;font-size:14px;margin-top:5px;margin-left:5px;'>"+results[0].address_components[1].long_name+" "+results[0].address_components[0].long_name+"<br />"+results[0].address_components[6].long_name+" "+results[0].address_components[2].long_name+"</p>"})
          }
        } else {
          alert("Geocoder failed due to: " + status);
        }
      });
    }
	}
	<?php }
	else{ ?>
    var latlng = new google.maps.LatLng(<?php echo $dades2["coordenades"]; ?>);
	var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapa"), myOptions);
	var marker = new google.maps.Marker({
		  position: latlng,
		  map: map,
		  icon: "img/marcador.png"
	  });
	  if (geocoder) {
		  
    

      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            infoBox = new InfoBox({latlng: marker.getPosition(), map: map,inner:"<p style='color:#FFF;font-size:14px;margin-top:5px;margin-left:5px;'>"+results[0].address_components[1].long_name+" "+results[0].address_components[0].long_name+"<br />"+results[0].address_components[6].long_name+" "+results[0].address_components[2].long_name+"</p>"})
          }
        } else {
          alert("Geocoder failed due to: " + status);
        }
      });
    }
    <?php } ?>
	





</script>
</body>
</html>